package cn.itcast.util;

import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class WxUtil {

    // 微信开放参数
    private static String appid="wx3bdb1192c22883f3";

    private static String secret="db9d6b88821df403e5ff11742e799105";

    private static String accessTokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token";

    private static String userInfoUrl="https://api.weixin.qq.com/sns/userinfo";

    /**
     * 获取登录认证信息
     * 根据code获取accessToken和openId
     *
     * @param code
     * @return
     */
    public static Map<String, String> getAuthorization(String code) {
        String atUtl = accessTokenUrl + "?code=" + code + "&appid=" + appid + "&secret=" + secret + "&grant_type=authorization_code";
        Map<String, String> authorization = null;
        try {
            authorization = HttpUtils.sendGet(atUtl);
        } catch (Exception e) {
        }
        return authorization;
    }

    /**
     * 获取用户微信信息
     * 根据accessToken和openId获取用户微信信息
     *
     * @param accessToken
     * @param openId
     * @return
     */
    public static Map<String, String> getUserInfo(String accessToken, String openId) {
        String atUtl = userInfoUrl + "?access_token=" + accessToken + "&openid=" + openId;
        Map<String, String> userInfo = HttpUtils.sendGet(atUtl);
        return userInfo;
    }
}
